package com.cerosoft.utils;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;

import com.cerosoft.api.http.models.MediaInfo;
import com.cerosoft.api.http.models.MediaInfo.FileType;
import com.cerosoft.api.http.models.MediaInfo.MediaType;

public class MediaXMLHelper {

	public static LinkedList<MediaInfo> getFolderList(String input) {

		Log.d("test", "enter getFolderList input =" + input);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		DocumentBuilder builder;

		LinkedList<MediaInfo> result = new LinkedList<MediaInfo>();
		try {

			Log.d("test", "enter getFolderList 2");
			builder = factory.newDocumentBuilder();
			Document document = builder.parse(new InputSource(new StringReader(
					input)));

			Element rootElement = document.getDocumentElement();

			NodeList list = rootElement.getElementsByTagName("ShareFolder");

			int length = list.getLength();
			Log.d("test", "enter getFolderList 3 length=" + length);
			for (int i = 0; i < length; i++) {
				Element entry = (Element) list.item(i);
				MediaInfo info = new MediaInfo();
				info.setName(entry.getAttribute("name"));
				info.setPath(entry.getAttribute("path"));
				result.add(info);
			}
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Log.d("test", "exit getFolderList ---");
		return result;
	}

	public static LinkedList<MediaInfo> getFolderInfo(String input,
			final String basePath) {

		Log.d("test", "enter getFolderInfo input =" + input);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		DocumentBuilder builder;

		LinkedList<MediaInfo> result = new LinkedList<MediaInfo>();
		try {

			Log.d("test", "enter getFolderInfo 2");
			builder = factory.newDocumentBuilder();
			Document document = builder.parse(new InputSource(new StringReader(
					input)));

			Element rootElement = document.getDocumentElement();

			NodeList list = rootElement.getElementsByTagName("Item");

			int length = list.getLength();
			Log.d("test", "enter getFolderInfo 3 length=" + length);
			for (int i = 0; i < length; i++) {
				Element entry = (Element) list.item(i);
				MediaInfo info = new MediaInfo();
				String typeAttr = entry.getAttribute("type");
				if (typeAttr != null
						&& typeAttr.trim().toLowerCase().equals("file")) {
					info.setMediaType(MediaType.file);
				}

				info.setName(entry.getAttribute("name"));
				info.setPath(basePath + File.separator + info.getName());
				info.setParentPath(basePath);

				switch (info.getMediaType()) {
				case file: {
					// info.setSize(Long.parseLong(entry.getAttribute("size")));
					// info.setTitle(entry.getAttribute("title"));
					// info.setAlbum(entry.getAttribute("album"));
					// info.setArtist(entry.getAttribute("artist"));

					String fileType = entry.getAttribute("filetype");
					FileType type = null;
					if (fileType == null) {
						type = FileType.binary;
					} else {
						String temp = fileType.trim().toLowerCase();
						if (temp.equals("binary")) {
							type = FileType.binary;
						} else if (temp.equals("document")) {
							type = FileType.document;
						} else if (temp.equals("html")) {
							type = FileType.html;
						} else if (temp.equals("image")) {
							type = FileType.image;
						} else if (temp.equals("audio")) {
							type = FileType.audio;
						} else if (temp.equals("video")) {
							type = FileType.video;
						}
					}

					info.setFileType(type);

					break;
				}
				case folder: {
					break;
				}
				}
				result.add(info);
			}
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Log.d("test", "exit getFolderInfo ---");
		return result;
	}
}
